package cn.snoworld.dto;

import com.fasterxml.jackson.annotation.JsonAlias;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
@JsonInclude(JsonInclude.Include.NON_EMPTY)// 设置忽略空字段
@ApiModel(value = "通用返回对象",description = "统一的返回值格式")
public class CommonResult<T> {
    @JsonIgnore
    private boolean success;

    @ApiModelProperty("响应码")
    private int code;

    @ApiModelProperty("提示信息")
    @JsonAlias("msg")
    private String message;

    @ApiModelProperty("携带数据")
    private T data;

    public static <T> CommonResult<T> OK() {
        return new CommonResult<T>()
                .setSuccess(true)
                .setCode(20000)
                .setMessage("ok")
                .setData(null);
    }

    public static <T> CommonResult<T> OK(T data) {
        return new CommonResult<T>()
                .setSuccess(true)
                .setCode(20000)
                .setMessage("ok")
                .setData(data);
    }

    public static <T> CommonResult<T> FAIL() {
        return new CommonResult<T>()
                .setSuccess(false)
                .setCode(50000)
                .setMessage("fail")
                .setData(null);
    }

    public static <T> CommonResult<T> FAIL(T data) {
        return new CommonResult<T>()
                .setSuccess(false)
                .setCode(50000)
                .setMessage("fail")
                .setData(data);
    }
}
